<template>
  <div class="login">

    <div class="container">
      <div class="square">
        <ul>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>
      </div>
      <div class="circle">
        <ul>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="isLogin">
        <h3 class="title">围群博客用户平台</h3>
        
        <el-form-item prop="accountPhone">
          <el-input v-model="loginForm.accountPhone" type="text" auto-complete="off" placeholder="账号/邮箱"
            prefix-icon="el-icon-user-solid">
          </el-input>
        </el-form-item>
        <el-form-item prop="accountPass">
          <el-input v-model="loginForm.accountPass" placeholder="密码" show-password prefix-icon="el-icon-lock"
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <!-- <router-link class="emailLoginText"  @click.native="isEmailLogin = true" :to="'/login'">验证码登录</router-link> -->
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: left;" v-if="register">
            <router-link class="link-type" @click.native="changePass = true" :to="'/login'">忘记密码</router-link>
          </div>
          <div style="float: right;" v-if="register">
            <span>没有账号？</span><router-link class="link-type" @click.native="isLogin = false"
              :to="'/login'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>


      <el-form ref="regisForm" :model="regisForm" :rules="regisRules" class="regis-form" v-else>
        <h3 class="title" style="margin-bottom: 30px;">围群博客注册</h3>
        <el-form-item prop="phone">
          <el-input v-model="regisForm.phone" type="text" auto-complete="off" placeholder="账号"
            prefix-icon="el-icon-user-solid">
            <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
          </el-input>
        </el-form-item>
        <el-form-item prop="nickName">
          <el-input v-model="regisForm.nickName" type="text" auto-complete="off" placeholder="昵称"
            prefix-icon="el-icon-magic-stick">
          </el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input v-model="regisForm.pass" placeholder="密码" prefix-icon="el-icon-unlock">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="regisForm.checkPass" placeholder="确认密码" prefix-icon="el-icon-key">
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="registerbtn">
            <span v-if="!loading">立即注册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" @click.native="isLogin = true" :to="'/login'"><span
                class="el-icon-d-arrow-left"></span>返回登录</router-link>
          </div>
        </el-form-item>
      </el-form>

    </div>

    <el-dialog :visible.sync="isChangePass ? true : changePass" :width="'800px'" :height="'400px'">
      <div slot="title" class="header-title">
        <span class="title-name">
          <i class="el-icon-edit" style="margin-right:10px"></i>
        </span>
        <span class="title-age">密码找回</span>
      </div>
      <div class="changePass">
        <el-steps :active="active" finish-status="success">
          <el-step title="身份验证"></el-step>
          <el-step title="新密码"></el-step>
          <el-step title="修改成功"></el-step>
        </el-steps>
        <div class="changePassMiddle">
          <div class="changePassCode" v-if="active == 0">
            <el-input placeholder="请输入邮箱" v-model="bindEmail" maxlength="30" show-word-limit clearable></el-input>
            <el-input v-model="smsCode.sms" placeholder="验证码" style="width: 60%;top:30px;"></el-input>&nbsp;

            <el-button type="primary" :disabled="disable" :class="{ codeGeting: isGeting }"
              style="position:absolute;width: 32%;top:100px;" @click="getVerifyCode">{{ getCode }}</el-button>
            <div style="position:absolute;width: 100%;top:150px;color:rgb(230, 162, 60)" v-show="sendCode"><i
                class="el-icon-warning"></i>&nbsp;验证码已发送至您的邮箱，一分钟内有效，请注意查收!</div>
          </div>
          <div class="outNewPass" v-if="active == 1">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
              class="demo-ruleForm">
              <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off" clearable></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" clearable></el-input>
              </el-form-item>
            </el-form>
          </div>

          <div class="succesChangePass" v-if="active == 2" style="position: absolute;top:0px;left:20px">
            <el-result icon="warning" title="密码修改失败" subTitle="将检查网络后重新进行密码修改">
            </el-result>
          </div>
          <div class="succesChangePass" v-if="active == 3" style="position: absolute;top:0px;left:20px">
            <el-result icon="success" title="密码修改成功" :subTitle="subTitle ? subTitle : '请重新登录, 5 秒钟之后自动退出登录...'">
            </el-result>
          </div>
        </div>
        <div class="changePassBotton">
          <el-button type="info" style="margin-top: 12px;" @click="active--" v-if="active == 1 || active == 2"><i
              class="el-icon-arrow-left"></i>退回</el-button>
          <el-button type="danger" plain style="margin-top: 12px;" @click="next" v-if="active == 0"
            :disabled="!sendCode">下一步<i class="el-icon-arrow-right"></i></el-button>
          <el-button type="danger" plain style="margin-top: 12px;" @click="nextChangePass" v-if="active == 1">下一步<i
              class="el-icon-arrow-right"></i></el-button>
        </div>
      </div>
    </el-dialog>


    <!-- <el-dialog :visible.sync="isEmailLogin" :width="'800px'" :height="'400px'">
      <div slot="title" class="header-title">
        <span class="title-name">
          <i class="el-icon-message" style="margin-right:10px"></i>
        </span>
        <span class="title-age">Email登录</span>
      </div>
      <div class="changePass">
        <div class="changePassMiddle"  style="margin-bottom: 50px;">
          <div class="changePassCode">
            <el-input placeholder="请输入邮箱" v-model="bindEmail" maxlength="30" show-word-limit clearable></el-input>
            <el-input v-model="smsCode.sms" placeholder="验证码" style="width: 60%;top:30px;"></el-input>&nbsp;

            <el-button type="primary" :disabled="disable" :class="{ codeGeting: isGeting }"
              style="position:absolute;width: 32%;top:100px;" @click="getVerifyCode">{{ getCode }}</el-button>
            <div style="position:absolute;width: 100%;top:150px;color:rgb(230, 162, 60)" v-show="sendCode"><i
                class="el-icon-warning"></i>&nbsp;验证码已发送至您的邮箱，一分钟内有效，请注意查收!</div>
          </div>
        </div>
        <div class="changePassBotton">
          <el-button type="danger" plain style="margin-top: 12px;" @click="emailLogin" v-if="active == 0"
            :disabled="!sendCode">登录<i class="el-icon-arrow-right"></i></el-button>
        </div>
      </div>
    </el-dialog> -->

    <!-- <el-dialog :visible.sync="openBindEmail" :width="'800px'" :height="'400px'">
      <div slot="title" class="header-title">
        <span class="title-name">
          <i class="el-icon-message" style="margin-right:10px"></i>
        </span>
        <span class="title-age">绑定邮箱</span>
      </div>
      <div class="changePass">
        <div class="changePassMiddle">
          <div class="changePassCode">
            <div style="position:absolute;width: 150%;top:-90px;left:-60px;color:rgb(243, 147, 106)"><i
                class="el-icon-warning"></i>&nbsp;欢迎进行围群博客的注册，邮箱仅用于用户个人的账号维护，请放心注册！
            </div>
            <el-input placeholder="请输入邮箱" v-model="bindEmail" maxlength="30" show-word-limit clearable></el-input>
            <el-input v-model="emailCode" placeholder="验证码" style="width: 60%;top:30px;"></el-input>&nbsp;
            <el-button type="primary" :disabled="disable" :class="{ codeGeting: isGeting }"
              style="position:absolute;width: 32%;top:100px;" @click="getEmailCode">{{ getCode }}</el-button>
            <div style="position:absolute;width: 100%;top:150px;color:rgb(230, 162, 60)" v-show="sendCode"><i
                class="el-icon-warning"></i>&nbsp;验证码已发送，一分钟内有效，请注意查收!</div>
          </div>
          <el-button type="danger" style="margin-top: 100px;margin-left: 230px;" @click="registerbtn"
            :disabled="!isSendEmailCode">注册</el-button>
        </div>
      </div>
    </el-dialog> -->

    <Vcode :show="isShow" @success="success" @close="close" />

  </div>
</template>
    
<script>
import { Login } from '@/assets/api/user.js'
import { $register } from '@/assets/api/user.js'
import { $sendTextMail } from '@/assets/api/user.js'
import { $emailCodeIsCorrect } from '@/assets/api/user.js'
import { $findAccountPass } from '@/assets/api/user.js'
import Vcode from "vue-puzzle-vcode";
export default {
  name: "Login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      isEmailLogin:false,//是否是邮箱登录
      subTitle: '',
      ruleForm: {//重新输入密码
        pass: '',
        checkPass: '',
      },
      smsCode: {//密码修改短信验证
        phone: '',
        sms: '',
      },
      rules: {//两次输入密码的规则
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      changePass: false,
      isChangePass: false,
      count: 60,
      disable: false,
      active: 0,
      sendCode: false,//验证是否已经发送
      isGeting: false,//60秒内重新获取验证码
      getCode: '获取验证码',
      isSendEmailCode: false,//是否发送了验证码
      emailCode: "",//邮箱验证码
      openBindEmail: false,//绑定邮箱弹框
      bindEmail: "",//申请绑定的邮箱
      codeUrl: "",
      isShow: false, // 验证码模态框是否出现
      loginForm: {
        accountPhone: "",
        accountPass: "",
        imgCode: "",
        salt: false,
        rememberMe: false,//是否保存密码
      },
      loginRules: {
        accountPhone: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        accountPass: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      regisForm: {
        phone: "",
        pass: "",
        nickName: "",
        checkPass: '',
      },
      regisRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        pass: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 注册开关
      register: true,
      redirect: undefined,

      //是否是login页面
      isLogin: true,

      //忘记密码
      forgetPass: false,

    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getPhoneAndPass();
  },
  components: {
    Vcode
  },
  methods: {
    // emailLogin(){

    // },
    nextChangePass() {//进行密码修改
      if (this.ruleForm.pass == "" || this.ruleForm.pass !== this.ruleForm.checkPass) {
        this.$message({
          message: '检查密码输入',
          type: 'error',
        })
        return;
      }
      $findAccountPass(this.bindEmail, this.ruleForm.pass).then((res) => {
        if (res.data.success) {
          this.isChangePass = true;
          this.active = this.active + 2;
          var n = 5;
          var countDown = setInterval(() => {
            this.subTitle = "请重新登录, " + n + " 秒钟之后自动返回登录页面..."
            if (n <= 0) {
              clearInterval(countDown);
              location.reload();
            }
            n--;
          }, 1000);
        } else {
          this.$message({
            message: res.data.message,
            type: 'error',
          });
          this.active++;
        }
      })
    },
    next() {//跳转到修改修改密码阶段
      $emailCodeIsCorrect(this.bindEmail, this.smsCode.sms).then((res) => {
        if (res.data.success) {
          this.active++;
        } else {
          this.$message({
            message: res.data.message,
            type: 'error',
          })
        }
      })
    },
    getVerifyCode() {//获取验证码
      this.$message({
        type: 'success',
        message: '正在发送，请稍候'
      })
      this.disable = true;
      $sendTextMail(this.bindEmail).then((res) => {
        this.$message({
          message: res.data.message,
          type: res.data.success ? 'success' : 'error',
        })
        if (res.data.success) {
          this.sendCode = true;
          var countDown = setInterval(() => {
            if (this.count < 1) {
              this.isGeting = false;
              this.disable = false;
              this.getCode = '获取验证码';
              this.count = 60;
              clearInterval(countDown);
            } else {
              this.isGeting = true;
              this.disable = true;
              this.getCode = this.count-- + 's后重发';
            }
          }, 1000);
        }
      }).catch((err) => {
        console.log(err);
      })
    },
    openBindEmailbtn() {
      if (this.regisForm.accountPhone == "") {
        this.$message({
          type: 'warning',
          message: '账号为空'
        })
        return false;
      }

      if (this.regisForm.pass == "") {
        this.$message({
          type: 'warning',
          message: '密码为空'
        })
        return false;
      }
      if (this.regisForm.nickName == "") {
        this.$message({
          type: 'warning',
          message: '昵称为空'
        })
        return false;
      }
      if (this.regisForm.checkPass == "") {
        this.$message({
          type: 'warning',
          message: '确认密码为空'
        })
        return false;
      }
      if (this.regisForm.checkPass !== this.regisForm.pass) {
        this.$message({
          type: 'warning',
          message: '密码不一致'
        })
        return false;
      }
      return true;
    },
    registerbtn() {//点击注册
      if (this.openBindEmailbtn()) {
        this.$confirm('账号为'+ this.regisForm.phone+',一旦注册账号无法修改, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          $register(this.regisForm).then((res) => {
          this.message(res);
          if (res.data.success) {
            this.regisForm = {};
            this.isLogin = true;
          }
        })
        }).catch(() => {});
      
      }
    },

    getEmailCode() {//获取验证码
      this.$message({
        type: 'success',
        message: '正在发送，请稍候'
      })
      $sendTextMail(this.bindEmail).then((res) => {
        console.log(res);
        this.message(res);
        if (res.data.success) {
          this.isSendEmailCode = true;
          this.sendCode = true;

          var countDown = setInterval(() => {
            if (this.count < 1) {
              this.isGeting = false;
              this.disable = false;
              this.getCode = '获取验证码';
              this.count = 60;
              clearInterval(countDown);
            } else {
              this.isGeting = true;
              this.disable = true;
              this.getCode = this.count-- + 's后重发';
            }
          }, 1000);
        }
      }).catch((err) => {
        console.log(err);
      })
    },

    getPhoneAndPass() {
      if (JSON.parse(localStorage.getItem("loginForm"))) {
        this.loginForm = JSON.parse(localStorage.getItem("loginForm"));
      }

    },

    changeCheckCode(_img) {//验证码点击刷新
      _img.src = "checkCode?" + new Date().getTime();
    },

    // 用户通过了验证
    success(msg) {
      this.isShow = false; // 通过验证后，需要手动隐藏模态框
      if (this.isLogin) {
        Login(this.loginForm).then(res => {
          if (res.data.success) {

            this.$message({
              message: '登录成功,欢迎使用围群博客',
              type: 'success'
            })
            if (this.loginForm.rememberMe) {//记住账号密码
              localStorage.setItem("loginForm", JSON.stringify(this.loginForm));
            } else if (JSON.parse(localStorage.getItem("loginForm"))) {
              localStorage.removeItem("localStorage");
            }
            sessionStorage.setItem("token", JSON.stringify(res.data.data.token));
            this.$router.push({
              name: 'index'
            })
          } else if (res.data.code !== 401) {
            this.$message({
              message: res.data.message,
              type: 'error'
            })
          }
          this.loading = false;
        }).catch(err => {
          console.log(err);
        })
      }

    },
    // 用户点击遮罩层，应该关闭模态框
    close() {
      this.isShow = false;
      this.loading = false;
    },

    handleLogin() {
      this.loading = true;
      this.isShow = true;
    },
    handleregis() {
      if (this.regisForm.pass !== this.regisForm.checkPass) {
        this.$message({
          message: '两次密码不一致',
          type: 'error'
        })
        this.loading = false;
        return;
      }
      this.loading = true;
      this.isShow = true;
    }
  }
};
</script>
  
<style scoped>
.emailLoginText{
  float: right;
  font-size: 16px;
  margin-right: 15px;
}
.changePassMiddle {

  width: 300px;
  height: 100px;
  position: absolute;
  bottom: 180px;
  left: 28%;
  padding: 30px;
}

.changePassBotton {
  position: absolute;
  bottom: 100px;
  right: 10%;
}



.changePass {
  height: 350px;
  padding: 50px;
  padding-top: 20px;
}

.schedule {
  margin-top: -15px;
  position: relative;
}



.scheduleTable {
  position: absolute;
  top: 67px;
  height: 135px;
  margin-left: 100px;
}



.broder-shadow {
  border: solid 1px #d6d5d5;
  box-shadow: 1px 1px 3px #a2a1a1
}


.logoDes {

  width: 600px;
  border-radius: 10px;
  margin: 0 auto;
  height: 350px;
  margin-top: 30px;

}

.container button {
  width: 280px;
  height: 40px;
  margin: 15px auto 10px auto;
  border: none;
  background: linear-gradient(-200deg, #6eebed, #a9d4ed);
  color: #fff;
  font-weight: bold;
  letter-spacing: 8px;
  border-radius: 10px;
  cursor: pointer;
  /* 动画过渡 */
  transition: 1s;
}

.container button:hover {
  background: linear-gradient(-200deg, #aac2ee, #64eaf1);
  background-position-x: -350px;
}


ul li {
  position: absolute;
  border: 1px solid #fff;
  background-color: #fff;
  width: 30px;
  height: 30px;
  list-style: none;
  opacity: 0;
}

.square li {
  top: 40vh;
  left: 60vw;
  /* 执行动画：动画名 时长 线性的 无限次播放 */
  animation: square 10s linear infinite;
}

.square li:nth-child(2) {
  top: 80vh;
  left: 10vw;
  /* 设置动画延迟时间 */
  animation-delay: 2s;
}

.square li:nth-child(3) {
  top: 80vh;
  left: 85vw;
  /* 设置动画延迟时间 */
  animation-delay: 4s;
}

.square li:nth-child(4) {
  top: 10vh;
  left: 70vw;
  /* 设置动画延迟时间 */
  animation-delay: 6s;
}

.square li:nth-child(5) {
  top: 10vh;
  left: 10vw;
  /* 设置动画延迟时间 */
  animation-delay: 8s;
}

.circle li {
  bottom: 0;
  left: 15vw;
  /* 执行动画 */
  animation: circle 10s linear infinite;
}

.circle li:nth-child(2) {
  left: 35vw;
  /* 设置动画延迟时间 */
  animation-delay: 2s;
}

.circle li:nth-child(3) {
  left: 55vw;
  /* 设置动画延迟时间 */
  animation-delay: 6s;
}

.circle li:nth-child(4) {
  left: 75vw;
  /* 设置动画延迟时间 */
  animation-delay: 4s;
}

.circle li:nth-child(5) {
  left: 90vw;
  /* 设置动画延迟时间 */
  animation-delay: 8s;
}

/* 定义动画 */
@keyframes square {
  0% {
    transform: scale(0) rotateY(0deg);
    opacity: 1;
  }

  100% {
    transform: scale(5) rotateY(1000deg);
    opacity: 0;
  }
}

@keyframes circle {
  0% {
    transform: scale(0) rotateY(0deg);
    opacity: 1;
    bottom: 0;
    border-radius: 0;
  }

  100% {
    transform: scale(5) rotateY(1000deg);
    opacity: 0;
    bottom: 90vh;
    border-radius: 50%;
  }
}

.imgCode {
  width: 50%;
}

.login {
  /* 100%窗口高度 */
  height: 97vh;
  /* 弹性布局 居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  background: linear-gradient(200deg, #6eebed, #a9d4ed);
  /* 溢出隐藏 */
  overflow: hidden;
}

.title {

  margin: 0px auto 30px auto;
  margin-top: 25px;
  margin-bottom: 60px;
  text-align: center;
  color: #707070;
}


.container span {
  font-size: 14px;
}

.container a {
  color: plum;
  text-decoration: none;
}

.container a:hover {
  font-size: 17px;
  color: plum;
  text-decoration: none;
}

.login-form {
  border-radius: 10px;
  background-color: #fff;
  width: 350px;
  height: 400px;
  padding: 25px 25px 5px 25px;
  /* 阴影 */
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.regis-form {
  border-radius: 10px;
  background-color: #fff;
  width: 350px;
  height: 450px;
  padding: 25px 25px 5px 25px;
  /* 阴影 */
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
</style>
  
  
    
    